package com.ai.chat.controller;

import com.ai.chat.common.Result;
import com.ai.chat.model.dto.ChatRequestDTO;
import com.ai.chat.model.dto.ChatResponseDTO;
import com.ai.chat.service.ChatService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("/chat")
@RequiredArgsConstructor
@Tag(name = "聊天接口", description = "AI聊天相关操作")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ChatController {

    private final ChatService chatService;

    /**
     * 发送聊天消息
     */
    @PostMapping("/message")
    @Operation(summary = "发送聊天消息", description = "发送消息给AI并获取响应")
    public CompletableFuture<Result<ChatResponseDTO>> sendMessage(@RequestBody ChatRequestDTO request) {
        return chatService.sendMessage(request)
                .thenApply(chatResponse -> Result.success(chatResponse));
    }

    /**
     * 获取聊天历史
     */
    @GetMapping("/history/{sessionId}")
    @Operation(summary = "获取聊天历史", description = "根据会话ID获取历史消息")
    public Result<?> getChatHistory(@PathVariable String sessionId) {
        // 模拟返回聊天历史
        return Result.success("模拟聊天历史数据");
    }

    /**
     * 创建新会话
     */
    @PostMapping("/session")
    @Operation(summary = "创建新会话", description = "创建新的聊天会话")
    public Result<?> createSession() {
        // 模拟创建会话
        return Result.success("new_session_id_" + System.currentTimeMillis());
    }

    /**
     * 删除会话
     */
    @DeleteMapping("/session/{sessionId}")
    @Operation(summary = "删除会话", description = "根据会话ID删除会话")
    public Result<?> deleteSession(@PathVariable String sessionId) {
        // 模拟删除会话
        return Result.success(true);
    }
}